/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CL;

import EL.Accounts;
import EL.ReservationDetails;
import EL.ReservationDetailsPK;
import EL.RestaurantTables;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import sfsBeans.ReservationContractBLORemote;
import sfsBeans.ShoppingCartBLORemote;
import slsBeans.FoodBLORemote;
import slsBeans.OrderBLORemote;
import slsBeans.ReservationBLORemote;
import slsBeans.TableBLORemote;

/**
 *
 * @author Tian
 */
public class ReservationCO extends HttpServlet
{

    private ReservationBLORemote rReservation;
    private ReservationContractBLORemote rContract;
    private TableBLORemote rTable;

    @Override
    public void init() throws ServletException
    {
        try
        {
            super.init();
            String jndiFileName = this.getServletContext().getRealPath("/") + "/WEB-INF/jndi.properties";
            Properties props = new Properties();
            props.load(new FileInputStream(jndiFileName));
            InitialContext ctx = new InitialContext(props);
            rReservation = (ReservationBLORemote) ctx.lookup("ReservationBLO/remote");
            rContract = (ReservationContractBLORemote) ctx.lookup("ReservationContractBLO/remote");
            rTable = (TableBLORemote) ctx.lookup("TableBLO/remote");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String action = request.getParameter("action");
        if(action.equalsIgnoreCase("add2Contract"))
        {
            HttpSession session = request.getSession();
            Accounts account = (Accounts) session.getAttribute("acc");
            if (account != null)
            {
                String tableID = request.getParameter("id");               
                String quantity = request.getParameter("quantity");
                System.out.println("aaaa"+tableID+"bbbb"+quantity);
                ReservationDetailsPK pk = new ReservationDetailsPK("", Integer.parseInt(tableID));
                ReservationDetails resDetail = new ReservationDetails(pk);
                resDetail.setRestaurantTables(rTable.getByID(Integer.parseInt(tableID)));
                resDetail.setQuantity(Integer.parseInt(quantity));
                rContract.addTable2Contract(resDetail);

                ArrayList<ReservationDetails> contract = rContract.viewContract();
                session.setAttribute("contract", contract);
                response.sendRedirect("ReservationCO?action=viewContract");
            }
        }
        else if (action.equalsIgnoreCase("viewContract"))
        {
            HttpSession session = request.getSession();
            ArrayList<ReservationDetails> contract = (ArrayList<ReservationDetails>) session.getAttribute("contract");
            float total = 0;
            for (ReservationDetails resDetail : contract)
            {
                total += resDetail.getQuantity() * resDetail.getRestaurantTables().getDiningRooms().getRoomTypes().getSurcharge();
            }
            session.setAttribute("total", total);
            session.setAttribute("contract", contract);
            response.sendRedirect("us_ViewContract.jsp");
        }
        else if (action.equalsIgnoreCase("submit"))
        {
            HttpSession session = request.getSession();
            ArrayList<ReservationDetails> contract = (ArrayList<ReservationDetails>) session.getAttribute("contract");
            Accounts account = (Accounts) session.getAttribute("acc");
            String result = rReservation.add(account, contract);
            if(!result.equals(""))
            {
                request.setAttribute("resID", result);
                RequestDispatcher rd = request.getRequestDispatcher("us_ReservationSuccess.jsp");
                rd.forward(request, response);
            }

        }
        else if (action.equalsIgnoreCase("update"))
        {
            HttpSession session = request.getSession();
            ArrayList<ReservationDetails> contract = (ArrayList<ReservationDetails>) session.getAttribute("contract");
            Enumeration params = request.getParameterNames();
            while (params.hasMoreElements())
            {
                String paraName = (String) params.nextElement();
                String paraValue = request.getParameter(paraName);
                if (paraName.contains("txtQuantity") && !paraValue.equals("0"))
                {
                    int tableID = Integer.parseInt(paraName.split("_")[0]);
                    int quantity = Integer.parseInt(paraValue);
                    RestaurantTables t = rTable.getByID(tableID);
                    for (ReservationDetails resDetail : contract)
                    {
                        if (resDetail.getRestaurantTables().equals(t))
                        {
                            resDetail.setQuantity(quantity);
                            rContract.update(resDetail);
                        }
                    }
                }
            }
            contract = rContract.viewContract();
            session.setAttribute("contract", contract);
            response.sendRedirect("ReservationCO?action=viewContract");
        }
        else if(action.equalsIgnoreCase("delete"))
        {
            HttpSession session = request.getSession();
            ArrayList<ReservationDetails> contract = (ArrayList<ReservationDetails>) session.getAttribute("contract");
            String tableId = request.getParameter("id");
            RestaurantTables t = rTable.getByID(Integer.parseInt(tableId));
            for(ReservationDetails resDetail : contract)
            {
                if(resDetail.getRestaurantTables().equals(t))
                {
                    rContract.remove(resDetail);
                }
            }
            contract = rContract.viewContract();
            session.setAttribute("contract", contract);
            response.sendRedirect("ReservationCO?action=viewContract");
        }
        else if (action.equalsIgnoreCase("deal"))
        {
            HttpSession session = request.getSession();
            ArrayList<ReservationDetails> contract = (ArrayList<ReservationDetails>) session.getAttribute("contract");
            Accounts account = (Accounts) session.getAttribute("acc");
            String result = rReservation.add(account, contract);
            if(!result.equals(""))
            {
                request.setAttribute("resID", result);
                RequestDispatcher rd = request.getRequestDispatcher("us_ReservationSuccess.jsp");
                rd.forward(request, response);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}
